=begin pod :tag<perl6>

=TITLE Sets, Bags, and Mixes

=SUBTITLE Unordered collections of unique and weighted objects in Perl 6

Often you want to collect objects in a container but you do not care
about the order of these objects. For such cases, Perl 6 provides the
I<unordered> collection types L<Set|/type/Set>, L<SetHash|/type/SetHash>,
L<Bag|/type/Bag>, L<BagHash|/type/BagHash>, L<Mix|/type/Mix>, and
L<MixHash|/type/MixHash>. Being unordered, these containers can be more
efficient than L<Lists|/type/List> for looking up elements or dealing with
repeated items.

If you want to get the contained objects (elements) without duplicates
and you only care I<whether> an element is in the collection or not,
you can use a L<Set|/type/Set> or L<SetHash|/type/SetHash>. (If you want
to get rid of duplicates but still preserve order, take a look at the
L<unique|/routine/unique> routine for L<List|/type/List>.)

=begin comment
=defn  Set or SetHash
Collection of distinct objects
=end comment

If you want to keep track of the number of times each object appeared,
you can use a L<Bag|/type/Bag> or L<BagHash|/type/BagHash>. In these
Baggy containers each element has a weight (an unsigned integer)
indicating the number of times the same object has been included in the
collection. The types L<Mix|/type/Mix> and L<MixHash|/type/MixHash> are
similar, but they also allow fractional weights.

=begin comment
=defn  Bag or BagHash
Collection of distinct objects mapped to integer weights

=defn  Mix or MixHash
Collection of distinct objects mapped to real-number weights
=end comment

=for comment
TODO: Partial type graph showing only QuantHash, Setty, Baggy, Mixy, Set,
      SetHash, Bag, BagHash, Mix, MixHash.

L<Set|/type/Set>, L<Bag|/type/Bag>, and L<Mix|/type/Mix> are I<immutable> types.
Use the mutable variants L<SetHash|/type/SetHash>, L<BagHash|/type/BagHash>,
and L<MixHash|/type/MixHash> if you want to add or remove elements after the
container has been constructed.

The six collection classes C<Set>, C<SetHash>, C<Bag>, C<BagHash>, C<Mix>,
C<MixHash>, all share similar semantics.

For one thing, as far as they are concerned, identical objects refer to the
same element – where identity is determined using the L<WHICH|/routine/WHICH>
method (i.e. the same way that the L<===> operator checks identity). For value
types like C<Str>, this means having the same value; for reference types
like C<Array>, it means referring to the same object instance.

Secondly, they provide a Hash-like interface where the actual elements of
the collection (which can be objects of any type) are the 'keys', and the
associated weights are the 'values':

=table
                      value of $a{$b} if $b       value of $a{$b} if $b
    type of $a        is an element               is not an element
    -------------     ----------------------      ---------------------
    Set / SetHash     True                        False
    Bag / BagHash     a positive integer          0
    Mix / MixHash     a non-zero real number      0

=head1 Set/Bag Operators

=comment TODO: Update this after ab5tract's set/bag/mix operator redesign.

There are several infixes devoted to performing common operations on
L<Set|/type/Set>, such as unions and set differences. Other operations include
boolean checks, like whether an object is an element of a C<Set>, or whether
one C<Set> is a subset of another C<Set>.

These infixes can be written using the UTF-8 character that represents the
function (like L<∈|/routine/∈>, or L<∪|/routine/∪>), or they can be written with an
equivalent ASCII version like L<(elem)|/routine/(elem)> or
L<(^)|/routine/⊖>.

Most of the time, explicitly using C<Set> objects with these infixes is
unnecessary. All the infix operators will work on any objects of type
L<Any|/type/Any> for its arguments (e.g., L<List|/type/List>,
L<Array|/type/Array>, L<Mix|/type/Mix>, etc.) and coerce them to C<Set>
where needed.

In some cases, if the type of an argument is a L<Bag|/type/Bag>, the infix
operator will behave in a different but analogous way to the way it would
behave with only C<Set> arguments.

=head2 Operators that return C<Bool>

Operators below are all precedence "Chaining infix".

=head3 infix (elem)

    multi sub infix:<(elem)>($a, Any $b --> Bool)
    multi sub infix:<(elem)>($a, Set $b --> Bool)

X<Membership operator>.

Returns C<True> if C<$a> is an B<element> of C<$b>.

    say 2 (elem) (1, 2, 3).Set;              # OUTPUT: «True␤»
    say 4 (elem) (1, 2, 3).Set;              # OUTPUT: «False␤»

=head3 infix ∈

    only sub infix:<∈>($a, $b --> Bool)

Alternate membership operator.

Equivalent to L«(elem)|/routine/(elem)», at codepoint U+2208 (ELEMENT OF).

=head3 infix ∉

    only sub infix:<∉>($a, $b --> Bool)

X<Non-membership operator>.

Equivalent to C<!(elem)>, i.e., returns C<True> if C<$a> is not an element
of C<$b>, at codepoint U+2209 (NOT AN ELEMENT OF).

    say 2 !(elem) (1, 2, 3).Set;             # OUTPUT: «False␤»
    say 4 !(elem) (1, 2, 3).Set;             # OUTPUT: «True␤»

=head3 infix (cont)

    multi sub infix:<(cont)>(Any $a, $b --> Bool)
    multi sub infix:<(cont)>(Set $a, $b --> Bool)

X<Contains operator>.

Returns C<True> if C<$a> B<contains> C<$b> as an element.

    say (1, 2, 3).Set (cont) 2;              # OUTPUT: «True␤»
    say (1, 2, 3).Set (cont) 4;              # OUTPUT: «False␤»

=head3 infix ∋

    only sub infix:<∋>($a, $b --> Bool)

Alternate contains operator.

Equivalent to L«(cont)|/routine/(cont)», at codepoint U+220B (CONTAINS AS MEMBER).

=head3 infix ∌

    only sub infix:<∌>($a, $b --> Bool)

X<Does not contain operator>.

Equivalent to C<!(cont)>, i.e., returns C<True> if C<$a> does not contain
C<$b>, at codepoint U+220C (DOES NOT CONTAIN AS MEMBER).

    say (1, 2, 3).Set !(cont) 2;             # OUTPUT: «False␤»
    say (1, 2, 3).Set !(cont) 4;             # OUTPUT: «True␤»

=head3 infix (<=)

    multi sub infix:<<(<=)>>(Any $a, Any $b --> Bool)
    multi sub infix:<<(<=)>>(Setty $a, Setty $b --> Bool)

X<Subset of or equal to operator>.

Returns C<True> if C<$a> is a B<subset> or is equal to C<$b>, i.e., if all
the elements of C<$a> are elements of C<$b> and C<$a> is a smaller or equal
sized set than C<$b>.

    say (1, 2, 3).Set (<=) (3, 2, 1).Set;    # OUTPUT: «True␤»
    say (1, 3).Set (<=) (2, 1).Set;          # OUTPUT: «False␤»
    say ∅ (<=) (3, 2, 1).Set;                # OUTPUT: «True␤»

=head3 infix ⊆

    only sub infix:<⊆>($a, $b --> Bool)

Alternate subset of or equal to operator.

Equivalent to L«(<=)», at codepoint U+2286 (SUBSET OF OR EQUAL TO).

=head3 infix ⊈

    only sub infix:<⊈>($a, $b --> Bool)

X<Neither subset of nor equal to operator>.

Equivalent to C«!(<=)», at codepoint U+2288 (NEITHER A SUBSET OF NOR EQUAL TO).

    say (1, 2, 3).Set !(<=) (3, 2, 1).Set;   # OUTPUT: «False␤»
    say (1, 3).Set ⊈ (2, 1).Set;             # OUTPUT: «True␤»

=head3 infix (<)

    multi sub infix:<<(<)>>(Any $a, Any $b --> Bool)
    multi sub infix:<<(<)>>(Setty $a, Setty $b --> Bool)

X<Subset of operator>.

Returns C<True> if C<$a> is a B<strict subset> of C<$b>, i.e., that all the
elements of C<$a> are elements of C<$b> but C<$a> is a smaller set than
C<$b>.

    say (1, 2, 3).Set (<) (3, 2, 1).Set;     # OUTPUT: «False␤»
    say (1, 3).Set (<) (3, 2, 1).Set;        # OUTPUT: «True␤»
    say ∅ (<) (3, 2, 1).Set;                 # OUTPUT: «True␤»

=head3 infix ⊂

    only sub infix:<⊂>($a, $b --> Bool)

Alternate subset of operator.

Equivalent to L«(<)», at codepoint U+2282 (SUBSET OF).

=head3 infix ⊄

    only sub infix:<⊄>($a, $b --> Bool)

X<Not a subset of operator>.

Equivalent to C«!(<)», at codepoint U+2284 (NOT A SUBSET OF).

    say (1, 2, 3).Set !(<) (3, 2, 1).Set;    # OUTPUT: «True␤»
    say (1, 3).Set ⊄ (3, 2, 1).Set;          # OUTPUT: «False␤»

=head3 infix (>=)

    multi sub infix:<<(>=)>>(Any $a, Any $b --> Bool)
    multi sub infix:<<(>=)>>(Setty $a, Setty $b --> Bool)

X<Superset of or equal to operator>.

Like L«(<=)» with reversed arguments. Returns C<True> if C<$a> is a
B<superset> of or equal to C<$b>.

    say (1, 2, 3).Set (>=) (3, 2, 1).Set;    # OUTPUT: «True␤»
    say (1, 3).Set (>=) (3, 2, 1).Set;       # OUTPUT: «False␤»
    say ∅ (>=) (3, 2, 1).Set;                # OUTPUT: «False␤»

=head3 infix ⊇

    only sub infix:<⊇>($a, $b --> Bool)

Alternate superset of or equal to operator.

Equivalent to L«(>=)», at codepoint U+2287 (SUPERSET OF OR EQUAL TO).

=head3 infix ⊉

    only sub infix:<⊉>($a, $b --> Bool)

X<Neither a superset of nor equal to operator>.

Equivalent to C«!(>=)», at codepoint U+2289 (NEITHER A SUPERSET OF
NOR EQUAL TO).

    say (1, 2, 3).Set !(>=) (3, 2, 1).Set;   # OUTPUT: «False␤»
    say (1, 3).Set ⊉ (3, 2, 1).Set;          # OUTPUT: «True␤»

=head3 infix (>)

    multi sub infix:<<(>)>>(Any $a, Any $b --> Bool)
    multi sub infix:<<(>)>>(Setty $a, Setty $b --> Bool)

X<Superset of operator>.

Like L«(<)» with reversed arguments. Returns C<True> if C<$a> is a
B<strict superset> of C<$b>.

    say (1, 2, 3, 4).Set (>) (3, 2, 1).Set;  # OUTPUT: «True␤»
    say (1, 3).Set (>) (3, 2, 1).Set;        # OUTPUT: «False␤»
    say ∅ (>) (3, 2, 1).Set;                 # OUTPUT: «False␤»

=head3 infix ⊃

    only sub infix:<⊃>($a, $b --> Bool)

Alternate superset of operator.

Equivalent to L«(>)», at codepoint U+2283 (SUPERSET OF).

=head3 infix ⊅

    only sub infix:<⊅>($a, $b --> Bool)

X<Not a superset of operator>.

Equivalent to C«!(>)», at codepoint U+2285 (NOT A SUPERSET OF).

    say (1, 2, 3, 4).Set !(>) (3, 2, 1).Set; # OUTPUT: «False␤»
    say (1, 3).Set ⊅ (3, 2, 1).Set;          # OUTPUT: «True␤»

=head3 infix (<+)

    multi sub infix:<<(<+)>>(Any $a, Any $b --> Bool)
    multi sub infix:<<(<+)>>(Baggy $a, Baggy $b --> Bool)

X<Baggy subset of operator>.

Returns C<True> if C<$a> is a Baggy B<subset> of C<$b>, i.e., if all the
elements of C<$a> are in C<$b> and each element of C<$b> is weighed at
least as heavily as the element is in C<$a>.

    say (1, 2, 3).Bag (<+) (3, 2, 1).Bag;    # OUTPUT: «True␤»
    say (1, 2, 2, 3).Bag (<+) (3, 2, 1).Bag; # OUTPUT: «False␤»

=head3 infix ≼

    only sub infix:<≼>($a, $b --> Bool)

Alternate baggy subset of operator.

Equivalent to L«(<+)», at codepoint U+227C (PRECEDES OR EQUAL TO).

=head3 infix (>+)

    multi sub infix:<<(>+)>>(Baggy $a, Baggy $b --> Bool)
    multi sub infix:<<(>+)>>(Any $a, Any $b --> Bool)

X<Baggy superset of operator>.

Returns C<True> if C<$a> is a Baggy B<superset> of C<$b>, i.e., if all the
elements of C<$b> are in C<$a> and no element of C<$b> is weighted heavier
than that element is in C<$a>.

    say (1, 2, 3).Bag (>+) (3, 2, 1).Bag;    # OUTPUT: «True␤»
    say (1, 2, 2, 3).Bag (>+) (3, 2, 1).Bag; # OUTPUT: «True␤»
    say (1, 2).Bag (>+) (3, 2, 1).Bag;       # OUTPUT: «False␤»

=head3 infix ≽

    only sub infix:<≽>($a, $b --> Bool)

Alternate baggy superset of operator.

Equivalent to L«(>+)», at codepoint U+227D (SUCCEEDS OR EQUAL TO).

=head2 Operators that return C<Set> or C<Bag>

=head3 infix (|)

    only sub infix:<(|)>(**@p)


X<Union operator>. It is of precedence "Junctive Or".

Returns the B<union> of all its arguments. Generally, this creates a new
C<Set> that contains all the elements its arguments contain.

    <a a b c d> (|) <h g f e d c> (|) <i j> === set <a b c d e f g h i j>

If any of its arguments are C<Baggy>, it creates a new C<Bag> that contains
all the elements of the arguments, each weighed by the highest weight that
appeared for that element.

    bag(<a a b c a>) (|) bag(<a a b c c>) === bag(<a a a b c c>)

=head3 infix ∪

    only sub infix:<∪>(|p)

Alternate union operator. It is of precedence "Junctive or".

Equivalent to L«(V<|>)», at codepoint U+222A (UNION).

=head3 infix (&)

    only sub infix:<(&)>(**@p)

X<Intersection operator>. It is of precedence "Junctive and".

Returns the B<intersection> of all of its arguments. Generally, this creates
a new C<Set> that contains only the elements common to all of the arguments.

    =begin code :skip-test
    <a b c> (&) <b c d> === set <b c>
    <a b c d> (&) <b c d e> (&) <c d e f> === set <c d>
    =end code

If any of the arguments are C<Baggy>, the result is a new C<Bag> containing
the common elements, each weighted by the largest I<common> weight (which
is the minimum of the weights of that element over all arguments).

    =begin code
    bag(<a a b c a>) (&) bag(<a a b c c>) === bag(<a a b c>)
    =end code

=head3 infix ∩

    only sub infix:<∩>(|p)

Alternate intersection operator. It is of precedence "Junctive and".

Equivalent to L«(&)», at codepoint U+2229 (INTERSECTION).

=head3 infix (-)

    only sub infix:<(-)>(**@p)


X<Set difference operator>. It is of precedence "Junctive or".

Returns the B<set difference> of all its arguments. Generally, this returns
the C<Set> made up of all the elements the first argument has but the rest
don't, i.e., of all the elements of the first argument, minus the elements
from the other arguments.

If the first argument is C<Baggy>, this returns a C<Bag> that contains each
element of the first argument with its weight subtracted by the weight of
that element in each of the other arguments.

    =begin code :skip-test
    bag(<a a b c a d>) (-) bag(<a a b c c>) === bag(<a d>)
    bag(<a a a a c d d d>) (-) bag(<a b d a>) (-) bag(<d c>) === bag(<a a d>)
    =end code

=head3 infix ∖

    only sub infix:<<"\x2216">>(|p)

Alternate set difference operator. It is of precedence "Junctive or".

Equivalent to L«(-)».

=head3 infix (^)

    multi sub infix:<(^)>(Any $a, Any $b --> Setty)
    multi sub infix:<(^)>(Set $a, Set $b --> Setty)


X<Symmetric set difference operator>. It is of precedence "Junctive or".

Returns the B<symmetric set difference> of all its arguments, i.e., a C<Set>
made up of all the elements that C<$a> has but C<$b> doesn't and all the
elements C<$b> has but C<$a> doesn't. Equivalent to C<($a ∖ $b) ∪ ($b ∖
$a)>.

=head3 infix ⊖

    only sub infix:<⊖>($a, $b --> Setty)

Alternate symmetric set difference operator. It is of precedence "Junctive or".

Equivalent to L«(^)», at codepoint U+2296 (CIRCLED MINUS).

=head3 infix (.)

    only sub infix:<(.)>(**@p)

X<Baggy multiplication operator>. It is of precedence "Junctive and".

Returns the Baggy B<multiplication> of its arguments, i.e., a C<Bag> that
contains each element of the arguments with the weights of the element
across the arguments multiplied together to get the new weight.

    =begin code :skip-test
    <a b c> (.) <a b c d> === bag <a b c> # Since 1 * 0 == 0, in the case of 'd'
    bag(<a a b c a d>) (.) bag(<a a b c c>) === ("a"=>6,"c"=>2,"b"=>1).Bag
    =end code

=head3 infix ⊍

    only sub infix:<⊍>(|p)

Alternate baggy multiplication operator. It is of precedence "Junctive and".

Equivalent to L«infix (.)|(.)», at codepoint U+228D (MULTISET MULTIPLICATION).

=head3 infix (+)

    only sub infix:<(+)>(**@p)

X<Baggy addition operator>. It is of precedence "Junctive or".

Returns the Baggy B<addition> of its arguments, i.e., a C<Bag> that contains
each element of the arguments with the weights of the element across the
arguments added together to get the new weight.

    =begin code
    bag(<a a b c a d>) (+) bag(<a a b c c>) === ("a"=>5,"c"=>3,"b"=>2,"d"=>1).Bag
    =end code

=head3 infix ⊎

    only sub infix:<⊎>(|p)

Alternate baggy addition operator. It is of precedence "Junctive or".

Equivalent to L«(+)», at codepoint U+228E (MULTISET UNION).

=head2 term ∅

X<|∅>

Equivalent to set(), aka the X<empty set>, at codepoint U+2205 (EMPTY SET).

=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
